'use strict'

const fs = require('fs')
const router = require('koa-router')()

function FilePath(path) {
    let FilePaths = fs.readdirSync(path)
    let routerFiles = FilePaths.filter(item => {
        return item.endsWith('.js') && item != 'index.js'
    })
    return routerFiles
}

function createRouter(routerFiles) {
    routerFiles.forEach(item => {
        let tmpPath = __dirname + '/' + item;
        let obj = require(tmpPath)
        for (let i in obj) {
            let tmpArr = i.split(' ');
            let rMethod = tmpArr[0];
            let rPath = tmpArr[1];
            let rFunction = obj[i]
            console.log(rMethod);
            router[rMethod](rPath,rFunction)
        }
    })
}

let rou = FilePath(__dirname)
createRouter(rou)
module.exports = router